package com.ant.storage.controller;
import com.ant.storage.common.ResultCode;
import com.ant.storage.common.ResultPage;
import com.ant.storage.common.ResultVo;
import com.ant.storage.dto.DictionaryDto;
import com.ant.storage.handler.AntException;
import com.ant.storage.pojo.Dictionary;
import com.ant.storage.service.IDictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author melon
 * @since 2022-03-16
 */
@RestController
@RequestMapping("/dictionary")
@Api(tags = "数据字典接口")
public class DictionaryController {

    @Autowired
    private IDictionaryService dictionaryService;

    @ApiOperation(value = "获取数据字典数据列表")
    @GetMapping("/findDictionaryWhere")
    public ResultPage findDictionaryWhere(DictionaryDto dictionaryDto) {
        return ResultPage.success("获取成功",dictionaryService.Pagelist(dictionaryDto));
    }

    @ApiOperation(value = "添加")
    @PostMapping("/addDictionary")
    public ResultVo addDictionary(@RequestBody DictionaryDto dictionarydto) {
        Dictionary dictionary=new Dictionary();
        if(StringUtils.isEmpty(dictionarydto.getTypeName())){
            throw  new AntException(ResultCode.PARAM_IS_BLANK.getResultCode(),"名字不能为空");
        }
        dictionary.setTypeName(dictionarydto.getTypeName());
        dictionary.setTypeCode(dictionarydto.getTypeCode());
        dictionary.setSortNo(dictionarydto.getSortNo());
        dictionary.setRemark(dictionarydto.getRemark());
       // BeanUtils.copyProperties(dictionarydto,dictionary);
        dictionaryService.save(dictionary);
        return ResultVo.success("添加成功");
    }


    @ApiOperation(value = "修改")
    @PostMapping("/updateDictionary")
    public ResultVo updateDictionary(@RequestBody DictionaryDto dictionarydto) {
        Dictionary dictionary=new Dictionary();
        if(StringUtils.isEmpty(dictionarydto.getTypeName())){
            throw  new AntException(ResultCode.PARAM_IS_BLANK.getResultCode(),"名字不能为空");
        }
        dictionary.setId(dictionarydto.getId());
        dictionary.setTypeName(dictionarydto.getTypeName());
        dictionary.setTypeCode(dictionarydto.getTypeCode());
        dictionary.setTypeValue(dictionarydto.getTypeValue());
        dictionary.setSortNo(dictionarydto.getSortNo());
        dictionary.setRemark(dictionarydto.getRemark());

        // BeanUtils.copyProperties(dictionarydto,dictionary);
        dictionaryService.updateById(dictionary);
        return ResultVo.success("修改成功");
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("/delDictionary/{id}")
    public ResultVo delDictionary(@PathVariable(value = "id",required = false) Integer id) {
        boolean b = dictionaryService.removeById(id);
        if(b){
            return ResultVo.success("删除成功");
        }
        return ResultVo.success("删除成功");
    }

    @ApiOperation(value = "获取单项")
    @GetMapping("/findDictionaryType")
    public ResultVo findDictionaryType() {
        return ResultVo.success("获取成功",   dictionaryService.findDictionaryType());
    }


}
